﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Safe_Driving
{
    class Security
    {

        /// <summary>
        /// Permet de crypter un pass en MD5.
        /// </summary>
        /// <param name="passOriginal">Le mot de passe à crypter</param>
        /// <returns>Retourne le mot de passe crypté.</returns>
        public string EncodePassword(String passOriginal)
        {
            // Instancie le MD5CryptoServiceProvider, et calcule le hash d'après le pass original.
            Byte[] originalBytes = ASCIIEncoding.Default.GetBytes(passOriginal);
            Byte[] encodedBytes = new MD5CryptoServiceProvider().ComputeHash(originalBytes);

            String pass = BitConverter.ToString(encodedBytes);
            String aRetourner = String.Empty;
            for (int i = 0; i < pass.Length; i++)
            {
                if (!(pass[i].Equals('-')))
                    aRetourner = aRetourner + pass[i];
            }
            return aRetourner;
        }


        /// <summary>
        /// Permet de générer un mot de passe aléatoire (non crypté).
        /// </summary>
        /// <returns>Retourne le mot de passe aléatoire.</returns>
        public String GenereMotDePasse()
        {
            Char[] car = new Char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                                         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                                         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                                         '-', '_', '!', '?', ',', '%', '/', '*', '+', '&', '<', '>'
                                       };
            Random rnd = new Random();

            StringBuilder pass = new StringBuilder();
            for (int i = 0; i < 10; i++)
                pass.Insert(i, car[rnd.Next(0, 74)]);

            return pass.ToString();
        }

    }
}
